package top.meeting.room.common.config;

import com.jfinal.config.*;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;
import com.xiaoleilu.hutool.cron.CronUtil;
import top.jfinal.jwtTokenPlugin.interceptor.AuthInterceptor;
import top.meeting.room.common.factory.JKhaledMixedJsonFactory;
import top.meeting.room.common.factory.JKhaledRenderFactory;
import top.meeting.room.common.handler.CorsHandler;
import top.meeting.room.common.interceptors.ExceptionInterceptor;
import top.meeting.room.module.system.form.RegisterForm;
import top.meeting.room.module.system.model.User;
import top.meeting.room.module.system.services.impl.DepartmentService;
import top.meeting.room.module.system.services.impl.UserService;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/7/20
 * FOR : 核心配置文件
 */
public class MainConfig extends JFinalConfig {

    public static Prop db = null;
    public static String APP_ID = "";
    public static String APP_SECRET = "";
    public static Integer APP_AGENTID = 0;


    public static Prop wx = null;

    public static Prop getDb(String state) {
        if ("pro".equals(state)) {
            db = PropKit.use("pro.db.properties");
        }else {
            db = PropKit.use("dev.db.properties");
        }
        return db;
    }

    public static Prop getWx(String state){
        if ("pro".equals(state)) {
            wx =  PropKit.use("pro.wx.properties");
        }else {
            wx = PropKit.use("dev.wx.properties");
        }
        return wx;
    }

    /**
     * 常量配置
     *
     * @param me
     */
    @Override
    public void configConstant(Constants me) {
        me.setDevMode(MainConfig.db.getBoolean("dev"));
        me.setEncoding("UTF-8");
        me.setReportAfterInvocation(false);                     // 先打印日志在执行
        me.setJsonFactory(JKhaledMixedJsonFactory.me());        // 使用JKhaledMixedJson的混合Json工厂
        me.setRenderFactory(JKhaledRenderFactory.me());         // 使用重写的错误输出方式
        //       me.setJsonDatePattern("TimeStamp");                    // 添加时间戳输出格式
        APP_ID = wx.get("app_id");                         //读取app_id
        APP_SECRET = wx.get("app_secret");                 //读取app_secret
        APP_AGENTID = StrKit.isBlank(wx.get("app_agentid")) ? 0 : Integer.parseInt(wx.get("app_agentid")); //读取app_agentid
    }

    /**
     * 路由配置
     *
     * @param me
     */
    @Override
    public void configRoute(Routes me) {
        me.add(new ConfigRouter());
    }

    /**
     * 不使用模板引擎
     *
     * @param me
     */
    @Override
    public void configEngine(Engine me) {
    }

    /**
     * 插件配置
     *
     * @param me
     */
    @Override
    public void configPlugin(Plugins me) {
        ConfigPlugins.config(me);
    }

    /**
     * 拦截器配置
     *
     * @param me
     */
    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new ExceptionInterceptor());
        me.add(new AuthInterceptor());                  // 权限拦截器
    }

    /**
     * 处理器配置
     *
     * @param me
     */
    @Override
    public void configHandler(Handlers me) {
        me.add(new CorsHandler());//跨域允许
    }

    @Override
    public void afterJFinalStart() {

        CronUtil.start();
        super.afterJFinalStart();
        DepartmentService.me.fillDepartmentRole();
        User user = (User) UserService.me.login("wxadmin","admin");
        if(user == null){
            UserService.me.register(RegisterForm.builder()
                    .userName("wxadmin")
                    .password("admin")
                    .openId("admin")
                    .wxUserId(2017L).build());
        }
    }
}
